package com.jeomo.mem.starter.mapper;

import com.jeomo.base.starter.mapper.IBaseMapper;
import com.jeomo.mem.starter.entity.MemberCard;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 会员卡表 Mapper 接口
 *
 * @author Jackpot
 * @since 2020-10-22
 */
@Mapper
public interface MemberCardMapper extends IBaseMapper<MemberCard> {

	 String querySql =
			 "SELECT" +
			 " member_card.id," +
			 " member_card.top_org_code," +
			 " member_card.org," +
			 " member_card.member_no," +
			 " member_card.card_no," +
			 " member_card.phone," +
			 " member_card.mem_level," +
			 " member_card.mem_status," +
			 " member_card.open_time," +
			 " member_card.amount," +
			 " member_card.total_consume_amount," +
			 " member_card.total_consume_number," +
			 " member_card.points," +
			 " member_card.total_win_points," +
			 " member_card.version," +

			 " member.version as member_version," +
			 " member.`code`," +
			 " member.`NAME`," +
			 " member.member_phone," +
			 " member.email," +
			 " member.city," +
			 " member.address," +
			 " member.cer_type," +
			 " member.cer_number," +
			 " member.birthday," +
			 " member.sex," +
			 " member.reg_time," +
			 " member.reg_channel," +
			 " member.car_number," +
			 " member.wechat," +
			 " member.work_unit," +
			 " member.post " +
			 " FROM" +
			 " member_card" +
			 " JOIN member ON member_card.member_no = member.code";

	/**
	 * 根据主门店和手机号查询会员信息
	 * @param org
	 * @param phone
	 * @return 返回唯一的会员卡
	 */
	@Select(querySql +
			" WHERE member_card.phone = #{phone} AND member_card.org = #{org}")
	MemberCard queryMemCardByPhone(@Param("org") String org,@Param("phone")  String phone);

	/**
	 * 根据会员卡号查询会员信息
	 * @param cardNo
	 * @return 返回唯一的会员卡
	 */
	@Select(querySql +
			" WHERE member_card.card_no = #{cardNo}")
	MemberCard queryMemCardByNo(String cardNo);

	/**
	 * 根据手机号或会员卡号查询会员信息(组织隔离)
	 * @param org
	 * @param number	手机号/会员卡号
	 * @return
	 */
	@Select(querySql +
			" WHERE (member_card.phone = #{number} OR member_card.card_no = #{number}) AND member_card.org = #{org}")
	MemberCard queryMemCardByPhoneOrNo(@Param("org") String org, @Param("number") String number);


	/**
	 * 查询所有的会员-会员卡信息
	 * @return
	 */
	@Select(querySql)
	List<MemberCard> queryAllMemCards();

	/**
	 * 修改会员卡的当前积分和累加积分
	 * @param cardNo	会员卡号
	 * @param points	积分变动(可正可负)
	 * @param accPoints	累加积分
	 * @return
	 */
	@Update("update member_card as m set m.points=m.points+ #{points},m.total_win_points=m.total_win_points+ #{accPoints} " +
			" WHERE m.card_no = #{cardNo} and m.version=#{version}")
	int updateCardPoints(@Param("cardNo") String cardNo,@Param("version")Long version ,
						 @Param("points") int points,@Param("accPoints") int accPoints);


	/**
	 * 修改会员卡等级
	 * @param cardNo
	 * @param levelCode
	 * @return
	 */
	@Update("update member_card set mem_level=#{levelCode} WHERE card_no = #{cardNo}")
	int updateCardLevel(@Param("cardNo") String cardNo,@Param("levelCode")String levelCode);


	/**
	 * 修改会员卡的累计消费金额和消费次数
	 * @param cardNo
	 * @param version
	 * @param payAmount
	 * @return
	 */
	@Update("update member_card as m set m.total_consume_amount=m.total_consume_amount + #{payAmount}," +
			"m.total_consume_number=m.total_consume_number + 1 " +
			" WHERE m.card_no = #{cardNo} and m.version=#{version}")
	int updateCardAmount(@Param("cardNo") String cardNo,@Param("version")Long version ,
						 @Param("payAmount") double payAmount);

}
